﻿using System;
using PoznanEvents.Events;
using System.ServiceModel;

namespace PoznanEvents.Models
{
    public class EventModel
    {
        private const string DateFormat = "yyyy-MM-dd";
        private eventsPortTypeClient EventsClient;
        public event EventHandler<getCurrentDayEventsCompletedEventArgs> CurrentDayEventsCompleted;
        public event EventHandler<getEventsFromDateCompletedEventArgs> EventsFromDayCompleted;
        public event EventHandler<getEventsCompletedEventArgs> EventsRangeDateCompleted;
        public event EventHandler<getEventsToDateCompletedEventArgs> EventsToDateCompleted;

        public EventModel(string endpointConfigurationName = "eventsHttpsSoap11Endpoint")
        {
            EventsClient = new eventsPortTypeClient(endpointConfigurationName);
        }

        public void GetCurrentData()
        {
                EventsClient.getCurrentDayEventsAsync(new getCurrentDayEventsRequest());
                EventsClient.getCurrentDayEventsCompleted += CurrentDayEventsCompleted;
        }

        public void GetDataFromDay(DateTime dateFrom)
        {
            EventsClient.getEventsFromDateAsync(new getEventsFromDateRequest(dateFrom.ToString(DateFormat)));
            EventsClient.getEventsFromDateCompleted += EventsFromDayCompleted;
        }

        public void GetDataToDay(DateTime dateTo)
        {
            EventsClient.getEventsToDateAsync(new getEventsToDateRequest(dateTo.ToString(DateFormat)));
            EventsClient.getEventsToDateCompleted += EventsToDateCompleted;
        }

        public void GetDataDateRange(DateTime dateFrom, DateTime dateTo = new DateTime())
        {
            try
            {
                EventsClient.getEventsAsync(new getEventsRequest(dateFrom.ToString(DateFormat), dateTo.ToString(DateFormat)));
                EventsClient.getEventsCompleted += EventsRangeDateCompleted;
            }
            catch (EndpointNotFoundException err) { }
        }
    }
}
